package com.yuanian.wf.service.impl;

import com.yuanian.wf.context.process.EndProcessAfterContext;
import com.yuanian.wf.context.process.EndProcessBeforeContext;
import com.yuanian.wf.context.task.CompleteTaskAfterContext;
import com.yuanian.wf.context.task.CompleteTaskBeforeContext;
import com.yuanian.wf.event.process.EndProcessAfterEvent;
import com.yuanian.wf.event.process.EndProcessBeforeEvent;
import com.yuanian.wf.event.task.CompleteTaskAfterEvent;
import com.yuanian.wf.event.task.CompleteTaskBeforeEvent;
import com.yuanian.wf.exception.WfException;
import com.yuanian.wf.pojo.dto.CompleteTaskDto;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
public class TaskServiceImpl implements com.yuanian.wf.service.TaskService, ApplicationContextAware {

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void completeTask(CompleteTaskDto completeTaskDto) {
        completeTaskDto.getVariables().put("flag", completeTaskDto.getAct());
        String taskId = completeTaskDto.getTaskId();
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null) {
            throw new WfException("当前登录人员没有权限完成任务");
        }
        completeTaskDto.initTask(task);
        //设置业务数据的主键
        Object obj = completeTaskDto.getBusinessData();
        if (obj instanceof Map) {
            Object dataId = ((Map)obj).get("id");
            if (dataId != null) {
                completeTaskDto.setDataId(dataId.toString());
            }
        }

        this.applicationContext.publishEvent(new CompleteTaskBeforeEvent("任务完成前的事件", new CompleteTaskBeforeContext(completeTaskDto)));
        if (completeTaskDto.nextIsEnd()) {
            this.applicationContext.publishEvent(new EndProcessBeforeEvent("流程结束前的事件", new EndProcessBeforeContext(completeTaskDto)));
        }
        Map<String, Object> variables = completeTaskDto.getVariables();
        //提交任务
        taskService.complete(taskId, variables);
        this.applicationContext.publishEvent(new CompleteTaskAfterEvent("任务完成后的事件", new CompleteTaskAfterContext(completeTaskDto)));
        if (completeTaskDto.nextIsEnd()) {
            this.applicationContext.publishEvent(new EndProcessAfterEvent("流程结束前的事件", new EndProcessAfterContext(completeTaskDto)));
        }
    }

}
